<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@page import="com.liuya.safe.model.SafePrivilege" %>
<%@page import="com.liuya.safe.entitle.handler.DecisionEntitlementHandler" %>
<%@ include file="../common/taglib.jsp" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <link rel="shortcut icon" href="${contextPath}/style/def/favicon.ico">
    <%@ include file="../common/static.jsp" %>

        <%
I18N i18n=I18N.getInstance();
DecisionEntitlementHandler handler=(DecisionEntitlementHandler) request.getAttribute( "handler" );
String id=request.getParameter("id");

SafePrivilege pvlg=handler.getPrivilege();

String[] mainMenuHrefs=new String[]{
		"returnMain.do?oper=return&id="+id
};
String[] mainMenuTexts=new String[] {
		pvlg.getType()==0?i18n.say( "Privilege" ):i18n.say( "Non_role_privilege" )
};
String[] subMenuHrefs=new String[]{
		"javascript:savePolicy();",
		"/decisionEntitlementTest/decision.do?id="+id
};
String[] subMenuTexts=new String[]{
		i18n.say( "Save" ),
		i18n.say( "Test" )
};
request.setAttribute( "mainMenuHrefs", mainMenuHrefs );
request.setAttribute( "mainMenuTexts", mainMenuTexts );
request.setAttribute( "subMenuHrefs", subMenuHrefs );
request.setAttribute( "subMenuTexts", subMenuTexts );
%>

    <title><%=i18n.say("Decision_Privilege")%> -- <%=pvlg.getName() %>
    </title>
<body>
<jsp:include page="../common/header.jsp"></jsp:include>
<%@include file="../common/menu.jsp" %>

<div class="smallContainer" id="entitleDiv">
</div>

<div id="editEntitleDialog">
    <form id="entitleForm" method="post" action="?id=<%=id %>">
    </form>
</div>

<jsp:include page="../common/footer.jsp"></jsp:include>

<script type="text/javascript">
    jQuery(document).ready(function () {
        loadEntitle();

        // dialog
        jQuery("#editEntitleDialog").dialog({
            autoOpen: false,
            modal: true,
            hide: "fadeOut",
            position: 'middle',
            width: 450,
            height: 350,
            buttons: {
                "OK": function () {
                    jQuery('#entitleForm').submit();
                }
            }
        });

        var entitleFormOptions = {
            //beforeSubmit:  checkOrderColumnForm,
            success: afterUpdateEntitle
        };

        jQuery('#entitleForm').submit(function () {
            jQuery(this).ajaxSubmit(entitleFormOptions);
            return false;
        });
    });

    function loadEntitle() {
        jQuery("#entitleDiv").load("loadEntitlements.do?oper=loadEntitlements&id=<%=id%>");
    }

    function afterUpdateEntitle() {
        jQuery("#editEntitleDialog").dialog("close");
        loadEntitle();
    }

    function moveEntitle(direct, index) {
        jQuery.ajax({
            url: 'moveEntitle.do?oper=moveEntitle',
            type: 'POST',
            async: false,
            data: {"id": "<%=id%>", "index": index, "direct": direct},
            error: function () {
                alert('Error move group column');
            },
            success: function (xml) {
                loadEntitle();
            }
        });
    }

    function deleteEntitle(index) {
        jQuery.ajax({
            url: 'deleteEntitle.do?oper=deleteEntitle',
            type: 'POST',
            async: false,
            data: {"id": "<%=id%>", "index": index},
            error: function () {
                alert('Error delete group column');
            },
            success: function (xml) {
                loadEntitle();
            }
        });
    }

    function editEntitle(index) {
        $("#entitleForm").attr("action", "editEntitle.do?id=<%=id%>&index=" + index);
        jQuery("#entitleForm").load("getEntitlement.do?oper=getEntitlement&id=<%=id%>&index=" + index,
                function () {
                    jQuery("#editEntitleDialog").dialog("option", "title", '<%=i18n.say("Edit_policy")%>');
                    jQuery("#editEntitleDialog").dialog("open");
                });
    }

    function addEntitle() {
        $("#entitleForm").attr("action", "addEntitle.do?id=<%=id%>");
        jQuery("#entitleForm").load("getEntitlement.do?oper=getEntitlement&id=<%=id%>",
                function () {
                    jQuery("#editEntitleDialog").dialog("option", "title", '<%=i18n.say("Add_policy")%>');
                    jQuery("#editEntitleDialog").dialog("open");
                });
    }

    //------- save
    function savePolicy() {
        jQuery.ajax({
            url: 'save.do?oper=save',
            type: 'POST',
            data: {"id": "<%=id%>"},
            error: function () {
                alert('Error save policy');
            },
            success: function (xml) {
                alert("<%=i18n.say( "saved" )%>");
            }
        });
    }
</script>

</body>
</html>